 

var locObj;
var map;
var markerClusterer;
 $(document).ready(function(){
   jQuery.get('/watch/cfg', {deviceId: 'value1'}, function(data, textStatus, xhr) {
     $("#mTitle span").replaceWith('<span>位图监控-------------扫描时间:'+data.scanTime/1000/60+'分钟.服务电话:'+data.severNo+'</span>');
   });
   
 //所有图片自适应 
    $("img").each(function(){$(this).addClass("img-responsive")})
    //初始日期控件,本地化
    $("#datepicker").datepicker( {dateFormat: "yy-mm-dd", defaultDate: 0,onSelect: function(selectedDate ){if(jQuery.type(locObj) != "undefined"){locObj.trigger('click')}}})
    $("#datepicker").datepicker( $.datepicker.regional[ "zh_cn" ] );
    $("#map_text").click(map_text)
    $("a.deviceId").click(test)
    $('#changeNameDialog').on('show.bs.modal', function (e) {
      $("#deviceId").val(e.relatedTarget.nextElementSibling.value)
      $("#oldName").val(e.relatedTarget.parentElement.previousElementSibling.text)
     })

    $("#changeNameForm").click(function(event) {
        jQuery.post('/watch/updateName', {deviceId:$("#deviceId").val(),name:$("#name").val()}, function(data, textStatus, xhr) {
           if(data==="true"){
          $("a.deviceId:contains("+$("#oldName").val()+")").text($("#name").val())
          alert('更新成功')
         }
       });
       $('#changeNameDialog').modal('hide')
       return false;
    });
    $("#settingForm").click(function(event) {
    	 
    	 jQuery.post('/watch/cfg', {"scanTime": $("#scanTime").val(),"severNo":$("#severNo").val()}, function(data, textStatus, xhr) {
    	   if(data==="true"){
    	   	updateCfg()
    	   	alert('更新成功')
    	   }
    	 });
    	 $("#setting").modal('hide')
    	 return false;
    });
 });

var updateCfg=function (){
	 jQuery.get('/watch/cfg', {deviceId: 'value1'}, function(data, textStatus, xhr) {
     $("#mTitle span").replaceWith('<span>位图监控-------------扫描时间:'+data.scanTime/1000/60+'分钟.服务电话:'+data.severNo+'</span>');
   });
}

 
 var test= function (){
     locObj = $(this)
 	jQuery.ajax({
      url: $(this).attr('href')+'/'+$("#datepicker").val(),
      type: 'GET',
      dataType: 'json',
      complete: function(xhr, textStatus) {
        //called when complete
      },
      success: function(data, textStatus, xhr) {
      	$("#container table tbody ").empty()
      	if (typeof(markerClusterer) != "undefined")
      	{
      	markerClusterer.clearMarkers()
      }
      	var markers = [];
     jQuery.each(data,function(index, el) {
     	if(index==0){
     		map.centerAndZoom(new BMap.Point(el.X,el.Y),map.getZoom()); //设置中心点
     	}
     	var infoWindow = new BMap.InfoWindow("位置:"+el.AddrStr+"<br/>时间:"+el.LocTime.substring(11,16)+"<br/>电话:"+el.PhoneNumber);
     	var label = new BMap.Label(index+"",{offset:new BMap.Size(10,-10)});
      	var marker =new BMap.Marker(new BMap.Point(el.X,el.Y))
      	marker.setLabel(label);
     	marker.addEventListener("click", function(){          
      this.openInfoWindow(infoWindow);
         })
     	markers.push(marker)
     	   	    
     	$("#container table tbody ").append('<tr><td>'+index+'</td><td>'+el.AddrStr+'</td><td>'+el.LocTime.substring(11,16)+'</td><td>'+el.PhoneNumber+'</td></tr>')
     });
         	 
             markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});
      },
      error: function(xhr, textStatus, errorThrown) {
         alert(textStatus)
      }
    });
  return false;
 }

var map_text = function (){
	 
	$("#container_map").toggleClass("hidden")
	$("#container").toggleClass("hidden")
    

}

function initMap(){
map = new BMap.Map("container_map"); //创建地图实例
map.addControl(new BMap.ScaleControl()) //比例尺控件
map.addControl(new BMap.NavigationControl()) 
map.centerAndZoom('广州',14);
$("head").append('<script type="text/javascript" src="http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js"></script>')
$("head").append('<script type="text/javascript" src="http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js"></script>')
}
 